package com.whfc.openapi.api.emp.controller;

import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.emp.dto.AppEmpAttendRecordDTO;
import com.whfc.emp.dto.AppEmpGpsDTO;
import com.whfc.emp.dto.WxEmpCurveMapDTO;
import com.whfc.emp.dto.openapi.*;
import com.whfc.emp.param.openapi.*;
import com.whfc.entity.dto.emp.AttendStatDTO;
import com.whfc.fuum.dto.open.OpenApiClientDTO;
import com.whfc.openapi.api.emp.service.OpenApiEmpService;
import com.whfc.openapi.constant.OpenApiConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * @author sun_guodong
 * @description
 * @date 2020-06-22
 */
@RestController
@RequestMapping("/open/api/v1/emp")
public class OpenApiEmpController implements Serializable {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OpenApiEmpService openApiEmpService;

    @GetMapping("/list")
    public Result list(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台获取人员列表,deptId:{},pageNum:{},pageSize:{}", deptId, pageNum, pageSize);
        PageData<OpenApiEmpDTO> pageData = openApiEmpService.list(deptId, pageNum, pageSize);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/detail")
    public Result detail(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                         @RequestParam("empId") Integer empId) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台获取单个人员信息,deptId:{},empId:{}", deptId, empId);
        OpenApiEmpDTO data = openApiEmpService.detail(empId);
        return ResultUtil.success(data);
    }


    @PostMapping("/add")
    public Result add(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                      @Validated @RequestBody OpenApiAddEmpParam param) {
        Integer deptId = openApiClientDTO.getDeptId();
        param.setDeptId(deptId);
        param.setProjectId(deptId);
        logger.info("开放平台添加人员,deptId:{},param:{}", deptId, param.toString());
        OpenApiEmpDTO data = openApiEmpService.add(param);
        return ResultUtil.success(data);
    }


    @PostMapping("/edit")
    public Result edit(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                       @Validated @RequestBody OpenApiEditEmpParam param) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台修改人员,deptId:{},param:{}", deptId, param.toString());
        openApiEmpService.edit(param);
        return ResultUtil.success();
    }

    @PostMapping("/del/{empId}")
    public Result del(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                      @PathVariable("empId") Integer empId) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台删除人员,deptId:{},empId:{}", deptId, empId);
        openApiEmpService.del(empId);
        return ResultUtil.success();
    }

    @PostMapping("/bind")
    public Result bind(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                       @Validated @RequestBody OpenApiBindEmpParam param) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台人员绑定安全帽,deptId:{},param:{}", deptId, param.toString());
        openApiEmpService.bind(param);
        return ResultUtil.success();
    }

    @PostMapping("/unbind")
    public Result unbind(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                         @Validated @RequestBody OpenApiBindEmpParam param) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台人员解绑安全帽,deptId:{},param:{}", deptId, param.toString());
        openApiEmpService.unbind(param);
        return ResultUtil.success();
    }

    @GetMapping("/device/data")
    public Result deviceData(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                             @RequestParam("empId") Integer empId) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台获取人员硬件数据,deptId:{},empId:{}", deptId, empId);
        OpenApiEmpDTO data = openApiEmpService.deviceData(empId);
        return ResultUtil.success(data);
    }

    @PostMapping("/enter")
    public Result enter(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                        @Validated @RequestBody OpenApiEmpEnterParam param) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台人员进场,deptId:{},param:{}", deptId, param.toString());
        openApiEmpService.enter(param);
        return ResultUtil.success();
    }

    @PostMapping("/outer")
    public Result outer(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                        @Validated @RequestBody OpenApiEmpEnterParam param) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台人员退场,deptId:{},param:{}", deptId, param.toString());
        openApiEmpService.outer(param);
        return ResultUtil.success();
    }

    @GetMapping("/fence/list")
    public Result fenceList(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台查询电子围栏列表,deptId:{}", deptId);
        ListData<OpenApiFenceDTO> listData = openApiEmpService.fenceList(deptId);
        return ResultUtil.success(listData);
    }

    @PostMapping("/fence/add")
    public Result fenceAdd(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                           @Validated @RequestBody OpenApiFenceAddParam param) {
        Integer deptId = openApiClientDTO.getDeptId();
        param.setDeptId(deptId);
        logger.info("开放平台添加电子围栏,deptId:{},param:{}", deptId, param.toString());
        OpenApiFenceDTO openApiFenceDTO = openApiEmpService.fenceAdd(param);
        return ResultUtil.success(openApiFenceDTO);
    }


    @PostMapping("/fence/edit")
    public Result fenceEdit(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                            @Validated @RequestBody OpenApiFenceEditParam param) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台编辑电子围栏,deptId:{},param:{}", deptId, param.toString());
        openApiEmpService.fenceEdit(param);
        return ResultUtil.success();
    }

    @PostMapping("/fence/del/{fenceId}")
    public Result fenceDel(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                           @PathVariable("fenceId") Integer fenceId) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台删除电子围栏,deptId:{},fenceId:{}", deptId, fenceId);
        openApiEmpService.fenceDel(fenceId);
        return ResultUtil.success();
    }

    @PostMapping("/fence/invoke")
    public Result fenceInvoke(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                              @Validated @RequestBody OpenApiFenceInvokeParam param) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台电子围栏添加人员,deptId:{},param:{}", deptId, param.toString());
        openApiEmpService.fenceInvoke(param);
        return ResultUtil.success();
    }

    @PostMapping("/fence/revoke")
    public Result fenceRevoke(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                              @Validated @RequestBody OpenApiFenceInvokeParam param) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台电子围栏删除人员,deptId:{},param:{}", deptId, param.toString());
        openApiEmpService.fenceRevoke(param);
        return ResultUtil.success();
    }

    @GetMapping("/attend/day")
    public Result attendDay(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                            @RequestParam("empId") Integer empId,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台查询人员每日考勤,deptId:{},empId:{},date:{}", deptId, empId, date);
        OpenApiEmpAttendDayDTO data = openApiEmpService.attendDay(empId, date);
        return ResultUtil.success(data);
    }

    @GetMapping("/attend/day/record")
    public Result attendRecord(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                               @RequestParam("empId") Integer empId,
                               @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台查询人员进出记录,deptId:{},empId:{},date:{}", deptId, empId, date);
        ListData<AppEmpAttendRecordDTO> data = openApiEmpService.attendRecord(empId, date);
        return ResultUtil.success(null);
    }


    @GetMapping("/gps")
    public Result gps(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                      @RequestParam("empId") Integer empId,
                      @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台查询人员运动轨迹,deptId:{},empId:{},date:{}", deptId, empId, date);
        ListData<AppEmpGpsDTO> data = openApiEmpService.gps(empId, date);
        return ResultUtil.success(null);
    }


    @GetMapping("/warn/list")
    public Result warnList(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                           @RequestParam("empId") Integer empId,
                           @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台查询人员每日安全帽预警,deptId:{},empId:{},date:{}", deptId, empId, date);
        ListData<OpenApiEmpWarnDTO> data = openApiEmpService.warnList(empId, date);
        return ResultUtil.success(data);
    }

    @GetMapping("/multi/curve/data")
    public Result multiCurveData(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台查询多人曲线图,deptId:{}", deptId);
        WxEmpCurveMapDTO data = openApiEmpService.getMultiEmpCurveData(deptId);
        return ResultUtil.success(data);
    }


    /**
     * 澳氹四桥-人员统计
     *
     * @param openApiClientDTO
     * @return
     */
    @GetMapping("/adsq/statistics")
    public List<StatItem> adsqStatistics(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台人员统计,deptId:{}", deptId);
        List<StatItem> data = openApiEmpService.adsqStatistics(deptId);
        return data;
    }

    /**
     * 澳氹四桥-人员统计
     *
     * @param openApiClientDTO
     * @return
     */
    @GetMapping("/adsq/localeInEmpNum")
    public List<StatItem> adsqLocaleInEmpNum(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台人员统计,deptId:{}", deptId);
        return openApiEmpService.adsqLocaleInEmpNum(deptId);
    }

    /**
     * 澳氹四桥-闸机统计
     *
     * @param openApiClientDTO
     * @return
     */
    @GetMapping("/adsq/facegate/statistics")
    public Result adsqFacegateStatistics(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                                         @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam(value = "date", required = false) Date date) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台闸机统计,deptId:{},date:{}", deptId, date);
        AttendStatDTO data = openApiEmpService.adsqFaceGateStatistics(deptId, date);
        return ResultUtil.success(data);
    }

}
